{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建字典去重，创建集合存储(不限制数值范围)\n",
    "def algorithm1(nums1, nums2):\n",
    "    table = {}                                  # 字典结构中键是唯一的，可以实现去重操作\n",
    "    for num in nums1:\n",
    "        table[num] = table.get(num, 0) + 1      \n",
    "    print(table.keys())\n",
    "    res = set()                                 # 题目没有限制数大小，所以不宜使用数组；但是集合set的坏处是占用大耗时慢\n",
    "    for num in nums2:\n",
    "        if num in table:\n",
    "            res.add(num)\n",
    "            del table[num]\n",
    "    return list(res)\n",
    "\n",
    "# 创建数组频率计数：数值完成去重和存储（限制数值范围在0-10）\n",
    "def algorithm2(nums1, nums2):\n",
    "    count1 = [0]*11\n",
    "    count2 = [0]*11                         # 假设数值范围是0到10，所以可以使用一个11大小的数组来存储列表数的频率\n",
    "    result = []\n",
    "    for i in range(len(nums1)):\n",
    "        count1[nums1[i]] +=1                  # nums中的元素取值大小对应于数组中的索引\n",
    "    for j in range(len(nums2)):\n",
    "        count2[nums2[j]] +=1\n",
    "    print(count1,count2)\n",
    "    for k in range(11):\n",
    "        if count1[k]*count2[k] > 0:          # 只有当两个频率表同一数都出现时，其对应的值乘积才会大于0\n",
    "            result.append(k)                 # 索引k对应实际值\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums1 = [1,2,2,1]\n",
    "nums2 = [2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys([1, 2])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[2]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "algorithm1(nums1, nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[2]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "algorithm2(nums1, nums2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
